package com.example.zhangruoyu_zoe.adapters

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.zhangruoyu_zoe.R
import com.example.zhangruoyu_zoe.data.Student

class StudentAdapter(var students: List<Student>) : RecyclerView.Adapter<StudentAdapter.StudentViewHolder>() {

    inner class StudentViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val idView: TextView = itemView.findViewById(R.id.idView)
        val nameView: TextView = itemView.findViewById(R.id.nameView)
        val genderView: TextView = itemView.findViewById(R.id.genderView)
        val classNameView: TextView = itemView.findViewById(R.id.classNameView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StudentViewHolder {
        val itemView = LayoutInflater.from(parent.context).inflate(R.layout.student_item, parent, false)
        return StudentViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: StudentViewHolder, position: Int) {
        val currentStudent = students[position]
        holder.idView.text = currentStudent.id
        holder.nameView.text = currentStudent.name
        holder.genderView.text = currentStudent.gender
        holder.classNameView.text = currentStudent.className
    }

    override fun getItemCount(): Int {
        return students.size
    }

    fun addStudent(student: Student) {
        students = students.toMutableList().apply { add(student) }
        notifyItemInserted(students.size - 1)
    }
}